package com.md.youyouhui.ui.user;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.support.v7.app.AlertDialog;

import com.md.youyouhui.R;
import com.md.youyouhui.databinding.ActivityAccountSafeBinding;
import com.md.youyouhui.net.Constants;
import com.md.youyouhui.net.response.UserInfoResponse;
import com.md.youyouhui.ui.BaseActivity;
import com.md.youyouhui.ui.login.LoginActivity;
import com.md.youyouhui.utils.AppManager;
import com.md.youyouhui.utils.SharedPreferencesUtils;
import com.md.youyouhui.utils.ToastUtils;
import com.md.youyouhui.view.MainViewModel;

/**
 * Created by AdministratorZJ
 * on 2019/4/9.
 */
public class UserSafeActivity extends BaseActivity {
    private ActivityAccountSafeBinding safeBinding;
    private MainViewModel viewModel;
    private String phone;

    @Override
    public void setContentLayout() {
        safeBinding = DataBindingUtil.setContentView(this, R.layout.activity_account_safe);
        viewModel = new MainViewModel();
    }

    @Override
    public void initView() {
        setSupportActionBar(safeBinding.toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public void initData() {
        //String phone = getIntent().getStringExtra("phone");
        //safeBinding.safePhone.setText(phone);
        safeBinding.safeRe.setOnClickListener(v -> startActivity(new Intent(ct, ReplacePhoneActivity.class).putExtra("phone", phone)));
        safeBinding.safeReMi.setOnClickListener(v -> startActivity(new Intent(ct, ReplacePwdActivity.class)));
        safeBinding.userExit.setOnClickListener(v -> new AlertDialog.Builder(ct).setMessage("退出登录").setPositiveButton("确定", (dialog, which) -> {
            SharedPreferencesUtils.saveString(UserSafeActivity.this, Constants.USER_TOKEN, "");
            AppManager.getAppManager().finishAllActivity();
            startActivity(new Intent(ct, LoginActivity.class));
            finish();
        }).setNegativeButton("取消", (dialog, which) -> dialog.dismiss()).show());



        safeBinding.logoutview.setOnClickListener(v -> {
            viewModel.logout().observe(this,objectResource -> {
                SharedPreferencesUtils.saveString(UserSafeActivity.this, Constants.USER_TOKEN, "");
                AppManager.getAppManager().finishAllActivity();
                startActivity(new Intent(ct, LoginActivity.class));
                finish();
            });
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        getUserInfo();
    }

    private void getUserInfo() {
        viewModel.getUserInfo().observe(this, userInfoResponse -> {
            assert userInfoResponse != null;
            switch (userInfoResponse.status) {
                case LOADING:
                    break;
                case SUCCESS:
                    UserInfoResponse data = userInfoResponse.data;
                    if (null == data) {
                        return;
                    }
                    phone = data.mobile;
                    safeBinding.safePhone.setText(data.mobile);
                    break;
                case ERROR:
                    ToastUtils.errMake(this, userInfoResponse.errorCode);
                    break;
                default:
                    break;
            }
        });
    }
}
